def startPosition(asc, length):
    if asc == True:
        return 0;
    else:
        return length - 1;

def endPosition(asc, length):
    if asc == True:
        return length;
    else:
        return -1;

def moveForward(asc, cur):
    if asc == True:
        return cur + 1;
    else:
        return cur - 1;

def medOf2Array(listA, listB):
    """

    :type listA: List[int]
    :type listB: List[int]
    :rtype: int
    """
    lenA = len(listA);
    lenB = len(listB);
    if lenA == 0:
        return listB[lenB // 2];
    elif lenB == 0:
        return listA[lenA // 2];
    else:
        ascA = listA[0] <= listA[lenA - 1];
        ascB = listB[0] <= listB[lenB - 1];
        listR = [];
        i = startPosition(ascA, lenA);
        j = startPosition(ascB, lenB);
        iE = endPosition(ascA, lenA);
        jE = endPosition(ascB, lenB);
        while(True):
            if i == iE and j == jE:
                print(i, j);
                break;
            elif j == jE or i != iE and listA[i] < listB[j]:
                listR.append(listA[i]);
                i = moveForward(ascA, i);
            elif i == iE or j != jE and listA[i] >= listB[j]:
                listR.append(listB[j]);
                j = moveForward(ascB, j);
            else:
                print("Err", i, j);
                break;
            
        return listR[(lenA + lenB) // 2];
